package github.liweiwei.sort;

import github.liweiwei.utils.SortUtil;

import java.util.Arrays;

/**
 * @Author: DRevelation
 * @Date: 2021/9/16 16:05
 */
public class BubbleSort {
    public static void main(String[] args) {
        int[] arr = {5, 2, 4, 1, 3};
        System.out.println("======排序前arr: " + Arrays.toString(arr));
        bubbleSort(arr);
        System.out.println("======排序后arr: " + Arrays.toString(arr));
    }

    private static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length-1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    SortUtil.swap(arr, j, j + 1);
                }
            }
            System.out.println("第" + i + "排序后: " + Arrays.toString(arr));
        }
    }
}
